package com.uchedao.vehicledetection.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.CountDownTimer;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.utils.ViewUtil;

public class MyEditView extends LinearLayout {

    private Context mContext;

    private ImageView iv_iocn;
    private EditText et_content;
    private TextView tv_sms;
    /**
     * 是否允许为空
     */
    private boolean isEmpty = false;
    /*按钮内容*/
    private boolean isShow = false;
    private boolean isCheck = false;
    private String txt;

    private TimeCount timeCount;

    public void setFocusableFalse() {
        if(et_content != null) {
            et_content.setFocusable(false);
            et_content.setFocusableInTouchMode(false);
        }
    }

    public MyEditView(Context context) {
        super(context);
        init(context);
    }

    public MyEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
        initData(attrs);
    }

    public MyEditView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
        initData(attrs);
    }

    private void init(Context context) {
        this.mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.custom_edit_view, this);
        iv_iocn = (ImageView) findViewById(R.id.iv_icon);
        tv_sms = (TextView) findViewById(R.id.tv_sms);
        et_content = (EditText) findViewById(R.id.et_content);
    }

    private void initData(AttributeSet attrs) {
        final TypedArray attributes = mContext.obtainStyledAttributes(attrs, R.styleable.MyEditView, 0, 0);

        int iconId = attributes.getResourceId(R.styleable.MyEditView_drawable_left, 0);
        if(iconId != 0)
            iv_iocn.setImageResource(iconId);

        String content_hint = attributes.getString(R.styleable.MyEditView_edit_hint);
        if (!TextUtils.isEmpty(content_hint)) {
            et_content.setHint(content_hint);
        }
        String content = attributes.getString(R.styleable.MyEditView_edit_content);
        if (!TextUtils.isEmpty(content)) {
            et_content.setText(content);
        }
        int inputType = attributes.getInt(R.styleable.MyEditView_inputType, InputType.TYPE_CLASS_TEXT);
        et_content.setInputType(inputType);
        int maxLength = attributes.getInt(R.styleable.MyEditView_edit_max_length, Integer.MAX_VALUE);
        InputFilter[] filters = {new InputFilter.LengthFilter(maxLength)};
        et_content.setFilters(filters);

        isEmpty = attributes.getBoolean(R.styleable.MyEditView_isEmpty, false);

        isShow = attributes.getBoolean(R.styleable.MyEditView_show_button, false);
        isCheck = attributes.getBoolean(R.styleable.MyEditView_btn_state, false);
        txt = attributes.getString(R.styleable.MyEditView_btn_text);

        initButton();
    }

    private void initButton() {
        if (isShow) {
            tv_sms.setVisibility(VISIBLE);
            if (TextUtils.isEmpty(txt)) {
                txt = mContext.getString(R.string.get_sms);
            }
            int bg_color = R.color.btn_green;
            tv_sms.setClickable(true);
            if (isCheck) {
                bg_color = R.color.btn_gray;
                tv_sms.setClickable(false);
                startThread();
            }
            tv_sms.setBackgroundColor(mContext.getResources().getColor(bg_color));
            tv_sms.setText(txt);
        } else {
            tv_sms.setVisibility(GONE);
        }
    }

    /**
     * 启动倒计时任务 60秒后自动修改isCheck状态为true
     */
    private void startThread() {
        if(timeCount != null){
            timeCount.cancel();
            timeCount = null;
        }
        timeCount = new TimeCount(60 * 1000, 1000);
        timeCount.start();
    }

    public void setCheck(boolean isCheck) {
        this.isCheck = isCheck;
        initButton();
    }

    public String getString() {
        String result = et_content.getText().toString();
        if (TextUtils.isEmpty(result) && !isEmpty) {
            ViewUtil.showEditError(et_content, mContext.getString(R.string.no_empty));
        }
        return result;
    }

    public EditText getEt_content() {
        return this.et_content;
    }

    public void setETContent(String txt) {
        if (!TextUtils.isEmpty(txt)) {
            et_content.setText(txt);
        }
    }

    public void setButton(OnClickListener onClickListener) {
        if (tv_sms != null) {
            tv_sms.setOnClickListener(onClickListener);
        }
    }

    /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {//计时完毕时触发
            setCheck(false);
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            tv_sms.setText(millisUntilFinished / 1000 + " 秒");
        }
    }
}
